import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/widgets/table/table.dart';
import 'package:kq_flutter_core_widget/widgets/table/render/kq_base_split_cell_render.dart';
import 'package:get/get.dart';

/// 表格组件Demo,拆分单元格
class TableSplitCellDemoPage extends StatefulWidget {
  const TableSplitCellDemoPage({super.key});

  @override
  State<StatefulWidget> createState() => _TableDemoPageState();
}

class _TableDemoPageState extends State<TableSplitCellDemoPage> {
  final TableController _tableController = TableController();
  int updateTime = DateTime.now().microsecondsSinceEpoch;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('拆分单元格'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          color: Colors.white,
          onPressed: Get.back,
        ),
      ),
      // body: Column(
      //   children: [
      //     Expanded(child: Text('xxxx')),
      //     Expanded(child: Column(
      //       children: [
      //         Expanded(child: Text('xxxx')),
      //         Expanded(child: Text('xxxx')),
      //       ],
      //     ))
      //   ],
      // ),
      body: _buildTable(),
    );
  }

  Widget _buildTable() {
    return KqTable(
      columnCount: 30,
      rowCount: 50,
      updateTime: updateTime,
      fixTopCount: 1,
      fixLeftCount: 1,
      fixRightCount: 1,
      syncInitAllColumnWidth: true,
      syncInitAllRowHeight: true,
      tableController: _tableController,
      cellBoxConstraints: (row, column) => const BoxConstraints(
          minWidth: 50, maxWidth: 200, minHeight: 50, maxHeight: 200),
      couldDragChangeWidth: (row, column) => row == 0,
      couldDragChangeHeight: (row, column) => column == 0,
      cellWidthMeasure: (int row, int column) => column == 2 ? 120 : 60,
      cellHeightMeasure: (int row, int column) =>
          column == 2 && row == 0 ? 60 : 30,
      cellRender: CustomCellRender(),
    );
  }
}

class CustomCellRender extends KqBaseSplitCellRender {
  @override
  Widget buildChildCell(BuildContext context, int row, int column,
      bool isLongPress, SplitCell child) {
    Widget result;
    if (child.value == '水平分割线') {
      result =
          Container(width: double.infinity, height: 0.5, color: Colors.grey);
    } else if (child.value == '垂直分割线') {
      result = Container(
        height: double.infinity,
        width: 0.5,
        color: Colors.grey,
        margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 4),
      );
    } else {
      result = Align(
          alignment: Alignment.center,
          child: Text(
            (child.value as String?) ?? '',
            style: const TextStyle(fontSize: 12),
          ));
    }
    return result;
  }

  @override
  SplitCell? getSplitCell(int row, int column) {
    if (row == 0) {
      if (column == 2) {
        return SplitCell(children: [
          SplitCell(
            value: '标题${column + 1}',
          ),
          const SplitCell(
            flex: null,
            value: '水平分割线',
          ),
          const SplitCell(axis: Axis.horizontal, children: [
            SplitCell(value: '副标题1'),
            SplitCell(
              flex: null,
              value: '垂直分割线',
            ),
            SplitCell(value: '副标题2')
          ])
        ]);
      }
      return SplitCell(value: '标题${column + 1}');
    }

    return SplitCell(value: '内容${column + 1}');
  }

  @override
  Widget build(BuildContext context, int row, int column, double width,
      double height, bool isLongPress) {
    var result =
        super.build(context, row, column, width - 20, height, isLongPress);
    result = Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10), child: result);
    result = Container(
        decoration: BoxDecoration(
            color: row == 0 ? Colors.grey[300]! : null,
            border: Border(
                left: column > 0
                    ? const BorderSide(color: Colors.grey, width: 0.5)
                    : BorderSide.none,
                bottom:
                    BorderSide(color: Colors.grey, width: row == 0 ? 1 : 0.5))),
        child: result);
    return result;
  }
}

class TableData extends SplitCell {
  TableData(
      {this.isTitle = false,
      super.children,
      super.flex,
      super.value,
      super.axis});

  final bool isTitle;
}
